package BlockingQueueDemo;

import lombok.SneakyThrows;

import java.util.concurrent.BlockingQueue;

/**
 * @author caihe
 * @date 2021/6/30 9:33
 */
public class MyProducer implements Runnable{
    
    private BlockingQueue<String> blockingQueue;
    
    public MyProducer(BlockingQueue<String> blockingQueue) {
        this.blockingQueue = blockingQueue;
    }
    
    @SneakyThrows
    @Override
    public void run() {
        while(true) {
            // 生产耗时
            Thread.sleep(1000);
            System.out.println("生成前库存：" + blockingQueue.size());             
            // 生产  - put 尝试往队列尾部添加元素，如果队列满了就阻塞当前线程知道添加成功
            blockingQueue.put("生产+1");
            System.out.println("生产后库存：" + blockingQueue.size());
        }
    }
}
